/**
 * 
 */
package com.lanswon.controller.admin;

import java.io.IOException;
import java.util.HashMap;
import java.util.Map;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.lanswon.common.util.StringUtil;
import com.lanswon.entity.UserVO;
import com.lanswon.service.UserService;

/**
 * 
 * @ClassName: AdminUserController.java
 * @Description: 该类的功能描述
 *
 * @version: v1.0.0
 * @author: zm
 * @date: 2018年5月16日 下午5:23:17
 *
 */
@Controller
@RequestMapping("admin/user")
public class AdminUserController {
	@Autowired
	UserService userService;

	@RequestMapping("/index")
	public String index() {
		return "admin/user/index";
	}

	@RequestMapping("/load")
	@ResponseBody
	public Map<String, Object> load(Integer page, Integer limit, String type, String content) throws IOException {
		
		Map<String, Object> load = userService.load(page, limit, type, content);
		return load;
	}

	@RequestMapping("/update")
	public String update(HttpServletRequest request, Long userId,ModelMap modelMap){
		
		UserVO user = userService.getUser(userId);
		if(user==null){
			modelMap.addAttribute("msg", "用户不存在或操作有误");
			return "admin/error";
		}
		modelMap.addAttribute("user", user);
		
		return "admin/user/userUpdate";
	}
	
	@RequestMapping("/updateUser")
	@ResponseBody
	public Map<String, Object> updateUser(UserVO user) throws IOException {
		Map<String, Object> viewMap = new HashMap<>();
		if(StringUtil.isEmpty(user.getUserName())||user.getUserId()==null||
				StringUtil.isEmpty(user.getPassword())){
			viewMap.put("code", 1);
			viewMap.put("msg", "修改信息不能为空");
			return viewMap;
		}
		user.setPassword(StringUtil.md5(user.getPassword()));
		System.out.println(user);
		userService.updateUserInfo(user);
		viewMap.put("code", 0);
		viewMap.put("msg", "修改成功");
		return viewMap;
	}
	
	@RequestMapping("/forbidden")
	@ResponseBody
	public void forbidden(Long userId) throws IOException {
		userService.forbidden(userId);
	}

	@RequestMapping("/restart")
	@ResponseBody
	public void restart(Long userId) throws IOException {
		userService.restart(userId);
	}

	@RequestMapping("/view")
	public String view(HttpServletRequest request, Long userId) throws ServletException, IOException {
		UserVO user = userService.getUser(userId);
		request.setAttribute("user", user);
		
		return "admin/user/userDetail";
	}
}